//引入API
import {createRouter,createWebHashHistory} from "vue-router"

//引入界面
// import Home from "../views/Home.vue"
// import About from "../views/About.vue"
//动态引入
const Home =()=>import("../views/Home.vue")
const About=()=>import("../views/About.vue") 
//引入用户中心
const UserCenter = ()=>import("../views/user-center/index.vue")
const UserProfile = ()=>import("../views/user-center/profile.vue")
const UserSettings = ()=>import("../views/user-center/Settings.vue")
//引入404页
const NotFound = () => import("../views/404.vue")
//引入Vuex页
const VuexPage = () => import("../views/Vuex.vue")
//注册路径及页面，定义路由规则
const routes = [
    {
        path:'/',
        redirect:'/home'
    },
    {
        path:'/home',
        name:'home',
        component: Home
    },
    {
        path:'/about',
        name:'about',
        component: About
    },
    {
        path:'/vuex',
        name:'vuex',
        component: VuexPage
    },
    {
        path:'/user',
        name:'userCenter',
        component: UserCenter,
        redirect:'/user/profile',
        children:[
            {
                path:'profile',
                name:'profile',
                component: UserProfile
            },
            {
                path:'settings',
                name:'settings',
                component: UserSettings
            },
        ]
    },
    {
        path:'/:pathMatch(.*)*',
        name:'notFound',
        component:NotFound
    }
]

//创建VueRouter的实例化对象
const router = createRouter({
    history: createWebHashHistory(),
    routes//key与value一致时写法
})

// router.beforeEach((to,from,next)=>{
//     console.log('beforeEach to',to);
//     console.log('beforeEach from',to);
//     next()

//     // if(to.name == 'userCenter') {
//     //     if(isLogin) {
//     //         next()
//     //     } else {
//     //         next({path:'/'})
//     //     }
//     // } else {
//     //     next()
//     // }
// })

// router.afterEach((to,from)=>{
//     console.log('afterEach to',to);
//     console.log('afterEach from',from);
//     //xxx
//})
export default router